  layui.config({
     base: CXL.ctxPath + '/layuiadmin/' //静态资源所在路径
  }).extend({
    index: 'lib/index' //主入口模块
  }).use(['index', 'form'], function(){
	  var form = layui.form;
	  var admin = layui.admin;
	  var table = layui.table;
	  var $ = layui.$;
      $(document).ready(function(){
        //建立websocket
        if('WebSocket' in window){
        //本地调试  ws://localhost:8080/refreshCarInfo
        //生产地址ws://192.168.2.36:8080/refreshCarInfo
            ws = new WebSocket('ws://localhost:8080/refreshCarInfo');
        }

        ws.onmessage = function(event){

            var res = $.parseJSON(event.data);

            //调用成功
            var data = res.data;
            if(1 == data){
                table.reload(Page.tableId);
            }

        }
      });


      var Page = {
                  tableId: 'sys-entrance-table',
                  url:CXL.ctxPath + '/sys/entrance/carInfoList',
                  queryUrl:CXL.ctxPath + '/sys/entrance/query/id',
                  addUrl:CXL.ctxPath + '/sys/entrance/add',
                  stopUrl:CXL.ctxPath + '/sys/entrance/stop',
                  initColumn:function(){
                      return [[
                            {type:'checkbox',name:'choose',title:'choose',width:'3%'},
                            {type : 'numbers',title : '序号',width:'3%'},
                            {field:'idKey',title:'idKey',hide:true,width:'1%'},
                            {field:'status',title:'状态',width:'6%'},
                            {field:'gateName',title:'闸口名',width:'8%'},
                            {field:'carLicense',title:'车牌号码',width:'8%'},
//                            {field:'detailsMsg',title:'详细信息',width:'10%'},
                            {field:'orderType',title:'废钢种类',width:'8%'},
                            {field:'sourceArea',title:'摆放位置',width:'8%'},
                            {field:'targetArea',title:'接受人',width:'8%'},
                            {field:'enterTime',title:'进入时间',width:'14%'},
                            {field:'exitTime',title:'离开时间',width:'14%'},
                            {title:'操作',width:'15%',toolbar: '#sys-entrance-tool'}
                      ]];
                  },
              };



    table.render({
        elem:'#' + Page.tableId,
        url:Page.url,
        height:'500px',
        page:true,
        limits:[3,5,7,10,20,30],
        limit:10,
        autoSort:false,
        where: Page.where,
        cols:Page.initColumn()
    });


    /**
     * 页面查询条件的重置
     */
    Page.resetAction = function() {
        $(".layui-input").val('');
    };

    /**
     * 编辑
     */
    Page.openEdit = function (data) {
        layer.open({
            type : 2,
            title : '编辑',
            shadeClose : true,
            shade : 0.8,
            area: ['700px', '700px'],
            content : Page.queryUrl + "?id=" + data.idKey,
            btn : [ '确定', '取消' ],
            yes : function(index, layero) {
                 //点击确认触发 iframe 内容中的按钮提交
                var submit = layero.find('iframe').contents().find("#form-btn-save");
                submit.click();

                //刷新表格
                table.render();
            }
        });
    };






    /**
     * 表头工具栏
     */
    table.on('tool(' + Page.tableId + ')', function(obj){
       //获得当前行数据
      var data = obj.data;
       //获得 lay-event 对应的值（也可以是表头的 event 参数对应的值）
      var layEvent = obj.event;

      //开闸操作
      if(layEvent === 'open'){
          var gateNameParam = data.gateName;

          if(gateNameParam == null || gateNameParam.length == 0){
                CXL.warn("该记录无道闸口，请确认道闸口");
                return;
          }
//          $('#open').val("开闸中");
          Page.openAction(gateNameParam,data.idKey,data.status);
      }
      //编辑操作
      else if(layEvent === 'edit'){
    	  Page.openEdit(data);
      }
    });



    /**
     * 搜索按钮事件
     */
    $('#btnSearch').click(function () {
    	Page.searchAction();
    });

    /**
     * 重置按钮事件
     */
    $('#btnReset').click(function () {
        Page.resetAction();
    });

    /**
     * 表头开闸按钮事件
     */
    $('#openEntrance').click(function () {
        var param = $("#gateName").val();
        if(param == null || param.length == 0){
            CXL.warn("请选择需要打开的道闸口");
            return;
        }
        Page.openAction(param,null,null);

    });


    /**
     * 表头开闸按钮事件
     */
    $('#btnMonitor').click(function () {
                $.ajax({
                   type: "post",
                   url: CXL.ctxPath + '/sys/entrance/monitor',
                   success: function(res) {
                       if(res.success) {
                           CXL.success(res.msg);
                       }else {
                           CXL.warn(res.msg);
                       }
                   }
                });
    });

    $("#add").click(function(){
        layer.open({
            type : 2,
            title : '编辑',
            shadeClose : true,
            shade : 0.8,
            area: ['700px', '700px'],
            content : Page.addUrl,
            btn : [ '确定', '取消' ],
            yes : function(index, layero) {
                 //点击确认触发 iframe 内容中的按钮提交
                var submit = layero.find('iframe').contents().find("#form-btn-save");
                submit.click();

                //刷新表格
                table.render();
            }
        });

    });


    Page.openAction = function(gateNameParam,idKey){
        $.ajax({
           type: "post",
           url: CXL.ctxPath + '/sys/entrance/openEntrance',
           data: {gateNameParam:gateNameParam,idKey:idKey,status:status},
           success: function(res) {
               if(res.success) {
                   CXL.success("打开道闸成功")
                   layer.open({
                     title: '道闸管理', // 标题内容
                     content: '道闸打开中...', // 弹窗内容
                     btn:'关闭',
                     yes: function(index, layero){ // 确认关闭按钮的回调函数
                          //刷新表格
                        Page.stopAction(res.obj);
                        Page.resetAction();
                        Page.searchAction();
                        layer.close(index); //再执行关闭

                     }
                   });
               }else {
                   CXL.warn(res.msg);
               }
           }
        });
    };


    /**
     * 页面查询
     */
    Page.searchAction = function() {
         var queryData = {};
         //查询字段
         queryData['carLicense'] = $("#carLicense").val();

         table.reload(Page.tableId, {where: queryData});
    };

    Page.stopAction = function(idKey){
        $.ajax({
           type: "post",
           url: Page.stopUrl,
           data: {idKey:idKey},
           success: function(res) {
               if(res.success) {
                   CXL.success(res.msg)
               }else {
                   CXL.warn(res.msg);
               }
           }
        });

    }














  })